﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Newtonsoft.Json.Linq;
using Serilog;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using TMI85303.UI.Messages;
using TMI85303.UI.UserControls;
using TMI85303.Utils;

namespace TMI85303.UI.ViewModels
{
    public class RegisterManager : Singleton<RegisterManager>
    {
        private RegisterManager()
        {
            Register = new();
        }
        public Register Register { get; set; }
    }

    public partial class Register: ObservableValidator
    {
        public Register()
        {
            //WeakReferenceMessenger.Default.Register<RefreshMTP, int>(this, (int)MesgToken.RegVMToken, OnRefreshMTP);
            WeakReferenceMessenger.Default.Register<UpdateRegisterMesg, int>(this, (int)MesgToken.RegVMToken, OnUpdateSingleReg);
            //WeakReferenceMessenger.Default.Register<WriteFileMTPDataConfig, int>(this, (int)MesgToken.MTP_DATA, OnWriteConfig);
            WeakReferenceMessenger.Default.Register<RefreshSpeedCurveMesg, int>(this, (int)MesgToken.RegVMToken, (_,_) =>drawPlot());

        }

        /*
        private void OnWriteConfig(object recipient, WriteFileMTPDataConfig message)
        {
            try
            {
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP8",((Reg72_Bit15_Bit14 << 14) | (Reg72_Bit13_Bit10 << 10) | (Reg72_Bit9_Bit4 << 4) | Reg72_Bit3_Bit0).ToString());
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP9",((Reg73_Bit15_Bit9 << 9) | Reg73_Bit8_Bit0).ToString());
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP10",(((MaxSpeed / 6000 * 255) << 8) | Reg74_Bit7_Bit0).ToString());
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP11", ((Reg75_Bit15_Bit8 << 8) | Reg75_Bit7_Bit0).ToString());
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP12", ((Reg76_Bit10_Bit8 << 8) | Reg76_Bit7_Bit0).ToString());
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP13", ((Reg77_Bit15_Bit12 << 12) | (Reg77_Bit11_Bit8 << 8) | (Reg77_Bit7_Bit4 << 4) | Reg77_Bit3_Bit0).ToString());
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP14", ((Reg78_Bit15_Bit8 << 8) | (Reg78_Bit7 << 7) | Reg78_Bit6_Bit0).ToString());
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP15", ((Reg79_Bit15_Bit8 << 8) | Reg79_Bit7_Bit0).ToString());
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP17", ((Reg81_Bit15_Bit12 << 12) | Reg81_Bit11_Bit0).ToString());
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP18", (
                    (Reg82_Bit15 << 15) | (Reg82_Bit14 << 14) | (Reg82_Bit13_Bit12 << 12) |
                    (Reg82_Bit10 << 10) | (Reg82_Bit9 << 9) | (Reg82_Bit8_Bit7 << 7) |
                    (Reg82_Bit6_Bit4 << 4) | (Reg82_Bit3 << 3) | (Reg82_Bit2 << 2) |
                    (Reg82_Bit1 << 1) | Reg82_Bit0).ToString());
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP19", (
                    (Reg83_Bit15 << 15) | (Reg83_Bit14 << 14) | (Reg83_Bit13 << 13) |
                    (Reg83_Bit12 << 12) | (Reg83_Bit11_Bit10 << 10) | (Reg83_Bit9_Bit8 << 8) |
                    (Reg83_Bit7 << 7) | (Reg83_Bit6 << 6) | (Reg83_Bit5 << 5) |
                    (Reg83_Bit4_Bit3 << 3) | (Reg83_Bit2 << 2) | (Reg83_Bit1 << 1) | Reg83_Bit0).ToString());
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP20", (
                    (Reg84_Bit14_Bit9 << 9) | (Reg84_Bit7 << 7) | (Reg84_Bit6_Bit5 << 5) |
                    (Reg84_Bit4 << 4) | (Reg84_Bit3 << 3) | (Reg84_Bit2 << 2) | Reg84_Bit0).ToString());
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP21", ((Reg85_Bit15_Bit8 << 8) | Reg85_Bit7_Bit0).ToString());
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP22", ((Reg86_Bit15_Bit8 << 8) | Reg86_Bit7_Bit0).ToString());
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP23", ((Reg87_Bit13_Bit8 << 8) | Reg87_Bit5_Bit0).ToString());
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP24", ((Reg88_Bit15_Bit12 << 12) | Reg88_Bit11_Bit0).ToString());
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP26", ((Reg90_Bit13_Bit6 << 6) | Reg90_Bit5_Bit0).ToString());
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP27", Reg91.ToString());
                IniFile.WriteSetting(PathHelper.IniPath, "MTP Data", $"MTP29", ((Reg93_Bit15_Bit8 << 8) | Reg93_Bit7_Bit0).ToString());

            }
            catch (Exception)
            {
                Log.Information("保存MTP数据发生异常");
            }

            if (ReadWrite.ConnectedOk)
            {
                if (ReadWrite.ReadReg(165, 2) != 0)
                {
                    ReadWrite.WriteReg(165, 0, 2);
                    ReadWrite.DisConnectDev();
                }
            }


        }*/

        private void OnUpdateSingleReg(object recipient, UpdateRegisterMesg message)
        {
            var index = message.Index32Reg;
            var val = message.NewData;
            switch(index)
            {
                case 8: 
                    {
                        Reg72_Bit15_Bit14 = val >> 14;
                        Reg72_Bit13_Bit10 = (val & 0x3C00) >> 10;
                        Reg72_Bit9_Bit4 = (val & 0x03F0) >> 4;
                        Reg72_Bit3_Bit0 = val & 0x000F;
                    } break;
                    case 9:
                    {
                        Reg73_Bit15_Bit9 = val >> 9;
                        Reg73_Bit8_Bit0 = val & 0x01FF;
                    }break;
                    case 10:
                    {
                        MaxSpeed = (val >> 8) * 60000 / 255;
                        Reg74_Bit7_Bit0 = val & 0x00FF;
                    }break;
                    case 11:
                    {
                        Reg75_Bit15_Bit8 = val >> 8;
                        Reg75_Bit7_Bit0 = val & 0x00FF;
                    }break;
                    case 12:
                    {
                        Reg76_Bit10_Bit8 = val >> 8;
                        Reg76_Bit7_Bit0 = val & 0x00FF;
                    }break;
                    case 13:
                    {
                        Reg77_Bit15_Bit12 = val >> 12;
                        Reg77_Bit11_Bit8 = (val & 0x0F00) >> 8;
                        Reg77_Bit7_Bit4 = (val & 0x00F0) >> 4;
                        Reg77_Bit3_Bit0 = val & 0x000F;
                    }break;
                    case 14:
                    {
                        Reg78_Bit15_Bit8 = val >> 8;
                        Reg78_Bit7 = ((val & 0x0080) == 0) ? 0 : 1;
                        Reg78_Bit6_Bit0 = val & 0x007F;
                    }break;
                    case 15:
                    {
                        Reg79_Bit15_Bit8 = val >> 8;
                        Reg79_Bit7_Bit0 = val & 0x00FF;
                    }break;
                    case 17:
                    {
                        Reg81_Bit15_Bit12 = val >> 12;
                        Reg81_Bit11_Bit0 = val & 0x0FFF;
                    }break;
                    case 18:
                    {
                        Reg82_Bit15 = ((val & 0x8000) == 0) ? 0 : 1;
                        Reg82_Bit14 = ((val & 0x4000) == 0) ? 0 : 1;
                        Reg82_Bit13_Bit12 = (val & 0x3000) >> 12;
                        Reg82_Bit10 = ((val & 0x0400) == 0) ? 0 : 1;
                        Reg82_Bit9 = ((val & 0x0200) == 0) ? 0 : 1;
                        Reg82_Bit8_Bit7 = (val & 0x0180) >> 7;
                        Reg82_Bit6_Bit4 = (val & 0x0070) >> 4;
                        Reg82_Bit3 = ((val & 0x0008) == 0) ? 0 : 1;
                        Reg82_Bit2 = ((val & 0x0004) == 0) ? 0 : 1;
                        Reg82_Bit1 = ((val & 0x0002) == 0) ? 0 : 1;
                        Reg82_Bit0 = ((val & 0x0001) == 0) ? 0 : 1;
                    }break;
                    case 19:
                    {
                        Reg83_Bit15 = ((val & 0x8000) == 0) ? 0 : 1;
                        Reg83_Bit14 = ((val & 0x4000) == 0) ? 0 : 1;
                        Reg83_Bit13 = ((val & 0x2000) == 0) ? 0 : 1;
                        Reg83_Bit12 = ((val & 0x1000) == 0) ? 0 : 1;
                        Reg83_Bit11_Bit10 = (val & 0x0C00) >> 10;
                        Reg83_Bit9_Bit8 = (val & 0x0300) >> 8;
                        Reg83_Bit7 = ((val & 0x0080) == 0) ? 0 : 1;
                        Reg83_Bit6 = ((val & 0x0040) == 0) ? 0 : 1;
                        Reg83_Bit5 = ((val & 0x0020) == 0) ? 0 : 1;
                        Reg83_Bit4_Bit3 = (val & 0x0018) >> 3;
                        Reg83_Bit2 = ((val & 0x0004) == 0) ? 0 : 1;
                        Reg83_Bit1 = ((val & 0x0002) == 0) ? 0 : 1;
                        Reg83_Bit0 = ((val & 0x0001) == 0) ? 0 : 1;
                    }break;
                    case 20:
                    {
                        Reg84_Bit14_Bit9 = (val & 0x7E00) >> 9;
                        Reg84_Bit7 = ((val & 0x0080) == 0) ? 0 : 1;
                        Reg84_Bit6_Bit5 = (val & 0x0060) >> 5;
                        Reg84_Bit4 = ((val & 0x0010) == 0) ? 0 : 1;
                        Reg84_Bit3 = ((val & 0x0008) == 0) ? 0 : 1;
                        Reg84_Bit2 = ((val & 0x0004) == 0) ? 0 : 1;
                        Reg84_Bit0 = ((val & 0x0001) == 0) ? 0 : 1;
                    }break; 
                    case 21:
                    {
                        Reg85_Bit15_Bit8 = (val & 0xFF00) >> 8;
                        Reg85_Bit7_Bit0 = val & 0x00FF;//21
                    }break;
                    case 22:
                    {
                        Reg86_Bit15_Bit8 = (val & 0xFF00) >> 8;
                        Reg86_Bit7_Bit0 = val & 0x00FF;
                    }break;
                    case 23:
                    {
                        Reg87_Bit13_Bit8 = (val & 0x3F00) >> 8;
                        Reg87_Bit5_Bit0 = val & 0x003F;
                    }break;
                    case 24:
                    {
                        Reg88_Bit15_Bit12 = (val & 0xF000) >> 12;
                        Reg88_Bit11_Bit0 = val & 0x0FFF;
                    }break;
                    case 26:
                    {
                        Reg90_Bit13_Bit6 = (val & 0x3FC0) >> 6;
                        Reg90_Bit5_Bit0 = val & 0x003F;
                    }break;
                    case 27: Reg91 = val;break;
                    case 29:
                    {
                        Reg93_Bit15_Bit8 = (val & 0xFF00) >> 8;
                        Reg93_Bit7_Bit0 = val & 0x00FF;
                    }break;
                default: break;
            }
        }


        #region 控件可用性属性
        [ObservableProperty]
        private bool speedVisible = false;

        [ObservableProperty]
        private bool brk_Mode1Enable = true;

        [ObservableProperty]
        private bool minSpeedEnable = false;

        [ObservableProperty]
        private bool maxDtyclpEnable = false;

        [ObservableProperty]
        private bool slpswrpmEnable = false;

        [ObservableProperty]
        private bool slpswdtyEnable = false;

        [ObservableProperty]
        private bool cmd_DisthEnable = true;

        [ObservableProperty]
        private bool minDtyclp2Enable = false;

        [ObservableProperty]
        private bool minSpeed2Enable = false;

        [ObservableProperty]
        private bool maxSpeed2Enable = false;


        [ObservableProperty]
        private bool tray_DtyUnitVisibility = false;

        [ObservableProperty]
        private bool enableDecaySel = false;

        [ObservableProperty]
        private bool enableIPD_Dtyramp = false;

        [ObservableProperty]
        private bool enableT_DITH = false;

        [ObservableProperty]
        private bool enableSTP_DITH = false;

        [ObservableProperty]
        private bool enableSPD_CL_EN = true;

        [ObservableProperty]
        private bool enableDir50 = true;

        #endregion

        #region 其它属性
        public int none_on_off;//不知道用来干嘛
        public bool InputDutyGreaterThanMaxDutyClamp_MaxDutyClamp = true;//本来是一个RadioButton按钮的选中值，但是没咋用，暂时这样

        /// <summary>
        /// 开闭环切换门限
        /// </summary>
        [ObservableProperty]
        private double switchThreshold =  Math.Round((19 * 40) * 96 * 0.390625 / 1000, 1);

        #endregion

        #region 寄存器参数
        #region slider寄存器
        // reg93_Bit7_Bit0 property
        private int reg93_Bit7_Bit0 = 16;
        [Required]
        [Range(0, 255)]
        public int Reg93_Bit7_Bit0
        {
            get => reg93_Bit7_Bit0;
            set
            {
                if (TrySetProperty(ref reg93_Bit7_Bit0, value, out _))
                {
                    Reg93_Bit7_Bit0Show = value + 1;
                }
            }
        }
        [ObservableProperty]
        private int reg93_Bit7_Bit0Show = 16 + 1;
        public int Reg93_Bit7_Bit0Max { get; set; } = 255;
        public int Reg93_Bit7_Bit0Min { get; set; } = 0;

        // reg93_Bit15_Bit8 property
        private int reg93_Bit15_Bit8 = 9;
        [Required]
        [Range(0, 255)]
        public int Reg93_Bit15_Bit8
        {
            get => reg93_Bit15_Bit8;
            set
            {
                if (TrySetProperty(ref reg93_Bit15_Bit8, value, out _))
                {
                    Reg93_Bit15_Bit8Show = Math.Round(value * 0.0625, 2);
                }
            }
        }
        [ObservableProperty]
        private double reg93_Bit15_Bit8Show = Math.Round(9 * 0.0625, 2);
        public int Reg93_Bit15_Bit8Max { get; set; } = 255;
        public int Reg93_Bit15_Bit8Min { get; set; } = 0;

        // reg91 property
        private int reg91 = 0;
        [Required]
        [Range(0, 32767)]
        public int Reg91
        {
            get => reg91;
            set
            {
                if(value < Reg81_Bit11_Bit0) value =  Reg81_Bit11_Bit0;
                if (TrySetProperty(ref reg91, value, out _))
                {
                    Reg91Show = value;
                }
            }
        }
        [ObservableProperty]
        private int reg91Show = 0;
        public int Reg91Max { get; set; } = 32767;
        public int Reg91Min { get; set; } = 0;

        // reg90_Bit5_Bit0 property
        private int reg90_Bit5_Bit0 = 0;
        [Required]
        [Range(0, 63)]
        public int Reg90_Bit5_Bit0
        {
            get => reg90_Bit5_Bit0;
            set
            {
                if (TrySetProperty(ref reg90_Bit5_Bit0, value, out _))
                {
                    Reg90_Bit5_Bit0Show = Math.Round(value * (100 / 63.0), 1);
                }
            }
        }
        [ObservableProperty]
        private double reg90_Bit5_Bit0Show = Math.Round(0 * (100 / 63.0), 1);
        public int Reg90_Bit5_Bit0Max { get; set; } = 63;
        public int Reg90_Bit5_Bit0Min { get; set; } = 0;

        // reg90_Bit13_Bit6 property
        private int reg90_Bit13_Bit6 = 0;
        [Required]
        [Range(0, 255)]
        public int Reg90_Bit13_Bit6
        {
            get => reg90_Bit13_Bit6;
            set
            {
                if (TrySetProperty(ref reg90_Bit13_Bit6, value, out _))
                {
                    Reg90_Bit13_Bit6Show = value == 0 ? "Single Slope Mode" : Math.Round(0.392 * value, 1).ToString();
                }
            }
        }
        [ObservableProperty]
        private string reg90_Bit13_Bit6Show = "Single Slope Mode";
        public int Reg90_Bit13_Bit6Max { get; set; } = 255;
        public int Reg90_Bit13_Bit6Min { get; set; } = 0;

        // reg88_Bit15_Bit12 property
        private int reg88_Bit15_Bit12 = 0;
        [Required]
        [Range(0, 15)]
        public int Reg88_Bit15_Bit12
        {
            get => reg88_Bit15_Bit12;
            set
            {
                if (TrySetProperty(ref reg88_Bit15_Bit12, value, out _))
                {
                    Reg88_Bit15_Bit12Show = Reg88_Bit15_Bit12 == 0 ? "Disabled" : (Reg88_Bit15_Bit12 == 1 ? "NO Retry" : value.ToString());
                }
            }
        }
        [ObservableProperty]
        private string reg88_Bit15_Bit12Show = "Disabled";
        public int Reg88_Bit15_Bit12Max { get; set; } = 15;
        public int Reg88_Bit15_Bit12Min { get; set; } = 0;

        // reg88_Bit11_Bit0 property
        private int reg88_Bit11_Bit0 = 0;
        [Required]
        [Range(0, 4095)]
        public int Reg88_Bit11_Bit0
        {
            get => reg88_Bit11_Bit0;
            set
            {
                if (value > MaxSpeed2) value = MaxSpeed2 - 1;
                if (TrySetProperty(ref reg88_Bit11_Bit0, value, out _))
                {
                    Reg88_Bit11_Bit0Show = value;
                }
            }
        }
        [ObservableProperty]
        private int reg88_Bit11_Bit0Show = 0;
        public int Reg88_Bit11_Bit0Max { get; set; } = 4095;
        public int Reg88_Bit11_Bit0Min { get; set; } = 0;

        // reg87_Bit5_Bit0 property
        private int reg87_Bit5_Bit0 = 12;
        [Required]
        [Range(0, 63)]
        public int Reg87_Bit5_Bit0
        {
            get => reg87_Bit5_Bit0;
            set
            {
                if (value > Reg87_Bit13_Bit8) value = Reg87_Bit13_Bit8;
                if (TrySetProperty(ref reg87_Bit5_Bit0, value, out _))
                {
                    Reg87_Bit5_Bit0Show = Math.Round(value * 1.56 + 1.37,2);
                }
            }
        }
        [ObservableProperty]
        private double reg87_Bit5_Bit0Show = 12 * 1.56 + 1.37;
        public int Reg87_Bit5_Bit0Max { get; set; } = 63;
        public int Reg87_Bit5_Bit0Min { get; set; } = 0;

        // reg87_Bit13_Bit8 property
        private int reg87_Bit13_Bit8 = 25;
        [Required]
        [Range(0, 63)]
        public int Reg87_Bit13_Bit8
        {
            get => reg87_Bit13_Bit8;
            set
            {
                if (value > Reg84_Bit14_Bit9) value = Reg84_Bit14_Bit9;
                if (value < Reg87_Bit5_Bit0) value = Reg87_Bit5_Bit0;
                if (TrySetProperty(ref reg87_Bit13_Bit8, value, out _))
                {
                    Reg87_Bit13_Bit8Show = Math.Round(value * 1.56 + 1.37, 2);
                }
            }
        }
        [ObservableProperty]
        private double reg87_Bit13_Bit8Show = 25 * 1.56 + 1.37;
        public int Reg87_Bit13_Bit8Max { get; set; } = 63;
        public int Reg87_Bit13_Bit8Min { get; set; } = 0;

        // reg86_Bit7_Bit0 property
        private int reg86_Bit7_Bit0 = 125;
        [Required]
        [Range(0, 255)]
        public int Reg86_Bit7_Bit0
        {
            get => reg86_Bit7_Bit0;
            set
            {
                if (TrySetProperty(ref reg86_Bit7_Bit0, value, out _))
                {
                    Reg86_Bit7_Bit0Show = value * 32;
                }
            }
        }
        [ObservableProperty]
        private int reg86_Bit7_Bit0Show = 125 * 32;
        public int Reg86_Bit7_Bit0Max { get; set; } = 255;
        public int Reg86_Bit7_Bit0Min { get; set; } = 0;

        // reg86_Bit15_Bit8 property
        private int reg86_Bit15_Bit8 = 156;
        [Required]
        [Range(0, 255)]
        public int Reg86_Bit15_Bit8
        {
            get => reg86_Bit15_Bit8;
            set
            {
                if (TrySetProperty(ref reg86_Bit15_Bit8, value, out _))
                {
                    Reg86_Bit15_Bit8Show = value * 32;
                }
            }
        }
        [ObservableProperty]
        private int reg86_Bit15_Bit8Show = 156 * 32;
        public int Reg86_Bit15_Bit8Max { get; set; } = 255;
        public int Reg86_Bit15_Bit8Min { get; set; } = 0;

        // reg85_Bit7_Bit0 property
        private int reg85_Bit7_Bit0 = 63;
        [Required]
        [Range(0, 255)]
        public int Reg85_Bit7_Bit0
        {
            get => reg85_Bit7_Bit0;
            set
            {
                if (TrySetProperty(ref reg85_Bit7_Bit0, value, out _))
                {
                    Reg85_Bit7_Bit0Show = value * 32;
                }
            }
        }
        [ObservableProperty]
        private int reg85_Bit7_Bit0Show = 63 * 32;
        public int Reg85_Bit7_Bit0Max { get; set; } = 255;
        public int Reg85_Bit7_Bit0Min { get; set; } = 0;

        // reg85_Bit15_Bit8 property
        private int reg85_Bit15_Bit8 = 94;
        [Required]
        [Range(0, 255)]
        public int Reg85_Bit15_Bit8
        {
            get => reg85_Bit15_Bit8;
            set
            {
                if (TrySetProperty(ref reg85_Bit15_Bit8, value, out _))
                {
                    Reg85_Bit15_Bit8Show = value * 32;
                }
            }
        }
        [ObservableProperty]
        private int reg85_Bit15_Bit8Show = 94 * 32;
        public int Reg85_Bit15_Bit8Max { get; set; } = 255;
        public int Reg85_Bit15_Bit8Min { get; set; } = 0;

        // reg84_Bit14_Bit9 property
        private int reg84_Bit14_Bit9 = 38;
        [Required]
        [Range(0, 63)]
        public int Reg84_Bit14_Bit9
        {
            get => reg84_Bit14_Bit9;
            set
            {
                if (value < Reg87_Bit13_Bit8) value = Reg87_Bit13_Bit8;
                if (TrySetProperty(ref reg84_Bit14_Bit9, value, out _))
                {
                    Reg84_Bit14_Bit9Show = Math.Round(value * 1.56 + 1.37, 2);
                }
            }
        }
        [ObservableProperty]
        private double reg84_Bit14_Bit9Show = 38 * 1.56 + 1.37;
        public int Reg84_Bit14_Bit9Max { get; set; } = 63;
        public int Reg84_Bit14_Bit9Min { get; set; } = 0;

        // reg83_Bit4_Bit3 property
        private int reg83_Bit4_Bit3 = 0;
        [Required]
        [Range(0, 3)]
        public int Reg83_Bit4_Bit3
        {
            get => reg83_Bit4_Bit3;
            set
            {
                if (TrySetProperty(ref reg83_Bit4_Bit3, value, out _))
                {
                    Reg83_Bit4_Bit3Show = (value + 1) * 4;
                }
            }
        }
        [ObservableProperty]
        private int reg83_Bit4_Bit3Show = (0 + 1) * 4;
        public int Reg83_Bit4_Bit3Max { get; set; } = 3;
        public int Reg83_Bit4_Bit3Min { get; set; } = 0;

        // reg82_Bit6_Bit4 property
        private int reg82_Bit6_Bit4 = 1;
        [Required]
        [Range(0, 7)]
        public int Reg82_Bit6_Bit4
        {
            get => reg82_Bit6_Bit4;
            set
            {
                if (TrySetProperty(ref reg82_Bit6_Bit4, value, out _))
                {
                    Reg82_Bit6_Bit4Show = value + 1;
                }
            }
        }
        [ObservableProperty]
        private int reg82_Bit6_Bit4Show = 1 + 1;
        public int Reg82_Bit6_Bit4Max { get; set; } = 7;
        public int Reg82_Bit6_Bit4Min { get; set; } = 0;

        // reg82_Bit13_Bit12 property
        private int reg82_Bit13_Bit12 = 1;
        [Required]
        [Range(0, 3)]
        public int Reg82_Bit13_Bit12
        {
            get => reg82_Bit13_Bit12;
            set
            {
                if (TrySetProperty(ref reg82_Bit13_Bit12, value, out _))
                {
                    switch (value)
                    {
                        case 0: Reg82_Bit13_Bit12Show = 200; break;
                        case 1: Reg82_Bit13_Bit12Show = 40; break;
                        case 2: Reg82_Bit13_Bit12Show = 80; break;
                        case 3: Reg82_Bit13_Bit12Show = 120; break;
                    }
                }
            }
        }
        [ObservableProperty]
        private int reg82_Bit13_Bit12Show = 40;
        public int Reg82_Bit13_Bit12Max { get; set; } = 3;
        public int Reg82_Bit13_Bit12Min { get; set; } = 0;

        // reg81_Bit15_Bit12 property
        private int reg81_Bit15_Bit12 = 0;
        [Required]
        [Range(0, 15)]
        public int Reg81_Bit15_Bit12
        {
            get => reg81_Bit15_Bit12;
            set
            {
                if (TrySetProperty(ref reg81_Bit15_Bit12, value, out _))
                {
                    if (value > 1)
                    {
                        Reg81_Bit15_Bit12Show = (value * 6.25).ToString();
                        Tray_DtyUnitVisibility = true;
                    }
                    else if (value == 0)
                    {
                        Reg81_Bit15_Bit12Show = "Sine Only";
                        Tray_DtyUnitVisibility = false;
                    }
                    else if (value == 1)
                    {
                        Reg81_Bit15_Bit12Show = "Trap Only";
                        Tray_DtyUnitVisibility = false;
                    }
                }
            }
        }
        [ObservableProperty]
        private string reg81_Bit15_Bit12Show = "Sine Only";
        public int Reg81_Bit15_Bit12Max { get; set; } = 15;
        public int Reg81_Bit15_Bit12Min { get; set; } = 0;

        // reg81_Bit11_Bit0 property
        private int reg81_Bit11_Bit0 = 0;
        [Required]
        [Range(0, 4095)]
        public int Reg81_Bit11_Bit0
        {
            get => reg81_Bit11_Bit0;
            set
            {
                if ((value > MaxSpeed) && MaxSpeed > 0) value = MaxSpeed - 1;

                if (SlpswrpmEnable && (value > Reg91)) value = Reg91;
                if (TrySetProperty(ref reg81_Bit11_Bit0, value, out _))
                {
                    Reg81_Bit11_Bit0Show = value;
                }
            }
        }
        [ObservableProperty]
        private int reg81_Bit11_Bit0Show = 0;
        public int Reg81_Bit11_Bit0Max { get; set; } = 4095;
        public int Reg81_Bit11_Bit0Min { get; set; } = 0;

        // reg79_Bit7_Bit0 property
        private int reg79_Bit7_Bit0 = 24;
        [Required]
        [Range(0, 255)]
        public int Reg79_Bit7_Bit0
        {
            get => reg79_Bit7_Bit0;
            set
            {
                if (TrySetProperty(ref reg79_Bit7_Bit0, value, out _))
                {
                    Reg79_Bit7_Bit0Show = Math.Round(25000000.0 / 2048.0 / value, 0);
                }
            }
        }
        [ObservableProperty]
        private double reg79_Bit7_Bit0Show = Math.Round(25000000.0 / 2048 / 24, 0);
        public int Reg79_Bit7_Bit0Max { get; set; } = 255;
        public int Reg79_Bit7_Bit0Min { get; set; } = 0;

        // reg79_Bit15_Bit8 property
        private int reg79_Bit15_Bit8 = 50;
        [Required]
        [Range(0, 255)]
        public int Reg79_Bit15_Bit8
        {
            get => reg79_Bit15_Bit8;
            set
            {
                if (TrySetProperty(ref reg79_Bit15_Bit8, value, out _))
                {
                    Reg79_Bit15_Bit8Show = value == 0 ? "忽略故障" : Math.Round(value * 0.1,2).ToString();
                }
            }
        }
        [ObservableProperty]
        private string reg79_Bit15_Bit8Show = "5";
        public int Reg79_Bit15_Bit8Max { get; set; } = 255;
        public int Reg79_Bit15_Bit8Min { get; set; } = 0;

        // reg78_Bit6_Bit0 property
        private int reg78_Bit6_Bit0 = 16;
        [Required]
        [Range(0, 127)]
        public int Reg78_Bit6_Bit0
        {
            get => reg78_Bit6_Bit0;
            set
            {
                if (TrySetProperty(ref reg78_Bit6_Bit0, value, out _))
                {
                    Reg78_Bit6_Bit0Show = value;
                }
            }
        }
        [ObservableProperty]
        private int reg78_Bit6_Bit0Show = 16;
        public int Reg78_Bit6_Bit0Max { get; set; } = 127;
        public int Reg78_Bit6_Bit0Min { get; set; } = 0;

        // reg78_Bit15_Bit8 property
        private int reg78_Bit15_Bit8 = 12;
        [Required]
        [Range(0, 255)]
        public int Reg78_Bit15_Bit8
        {
            get => reg78_Bit15_Bit8;
            set
            {
                if (TrySetProperty(ref reg78_Bit15_Bit8, value, out _))
                {
                    Reg78_Bit15_Bit8Show = value;
                }
            }
        }
        [ObservableProperty]
        private int reg78_Bit15_Bit8Show = 12;
        public int Reg78_Bit15_Bit8Max { get; set; } = 255;
        public int Reg78_Bit15_Bit8Min { get; set; } = 0;

        // reg77_Bit7_Bit4 property
        private int reg77_Bit7_Bit4 = 2;
        [Required]
        [Range(0, 15)]
        public int Reg77_Bit7_Bit4
        {
            get => reg77_Bit7_Bit4;
            set
            {
                if (TrySetProperty(ref reg77_Bit7_Bit4, value, out _))
                {
                    Reg77_Bit7_Bit4Show = value * 2 + 2;
                }
            }
        }
        [ObservableProperty]
        private int reg77_Bit7_Bit4Show = 2 * 2 + 2;
        public int Reg77_Bit7_Bit4Max { get; set; } = 15;
        public int Reg77_Bit7_Bit4Min { get; set; } = 0;

        // reg77_Bit3_Bit0 property
        private int reg77_Bit3_Bit0 = 5;
        [Required]
        [Range(0, 15)]
        public int Reg77_Bit3_Bit0
        {
            get => reg77_Bit3_Bit0;
            set
            {
                if (TrySetProperty(ref reg77_Bit3_Bit0, value, out _))
                {
                    Reg77_Bit3_Bit0Show = Math.Round(value * 4 + 3.8, 1);
                }
            }
        }
        [ObservableProperty]
        private double reg77_Bit3_Bit0Show = Math.Round((double)5 * 4 + 3.8, 1);
        public int Reg77_Bit3_Bit0Max { get; set; } = 15;
        public int Reg77_Bit3_Bit0Min { get; set; } = 0;

        // reg77_Bit15_Bit12 property
        private int reg77_Bit15_Bit12 = 6;
        [Required]
        [Range(0, 15)]
        public int Reg77_Bit15_Bit12
        {
            get => reg77_Bit15_Bit12;
            set
            {
                if (TrySetProperty(ref reg77_Bit15_Bit12, value, out _))
                {
                    Reg77_Bit15_Bit12Show = Math.Round((double)value * 4 + 3.8, 1);
                }
            }
        }
        [ObservableProperty]
        private double reg77_Bit15_Bit12Show = Math.Round((double)6 * 4 + 3.8, 1);
        public int Reg77_Bit15_Bit12Max { get; set; } = 15;
        public int Reg77_Bit15_Bit12Min { get; set; } = 0;

        // reg77_Bit11_Bit8 property
        private int reg77_Bit11_Bit8 = 6;
        [Required]
        [Range(0, 15)]
        public int Reg77_Bit11_Bit8
        {
            get => reg77_Bit11_Bit8;
            set
            {
                if (TrySetProperty(ref reg77_Bit11_Bit8, value, out _))
                {
                    Reg77_Bit11_Bit8Show = Math.Round((double)value * 4 + 3.8, 1);
                }
            }
        }
        [ObservableProperty]
        private double reg77_Bit11_Bit8Show = Math.Round((double)6 * 4 + 3.8, 1);
        public int Reg77_Bit11_Bit8Max { get; set; } = 15;
        public int Reg77_Bit11_Bit8Min { get; set; } = 0;

        // reg76_Bit7_Bit0 property
        private int reg76_Bit7_Bit0 = 25;
        [Required]
        [Range(0, 255)]
        public int Reg76_Bit7_Bit0
        {
            get => reg76_Bit7_Bit0;
            set
            {
                if (TrySetProperty(ref reg76_Bit7_Bit0, value, out _))
                {
                    Brk_Mode1Enable = value > 0;
                    Reg76_Bit7_Bit0Show = Math.Round(value * (100 / 255.0), 1);
                }
            }
        }
        [ObservableProperty]
        private double reg76_Bit7_Bit0Show = Math.Round(25 * (100 / 255.0), 1);
        public int Reg76_Bit7_Bit0Max { get; set; } = 255;
        public int Reg76_Bit7_Bit0Min { get; set; } = 0;

        // reg76_Bit10_Bit8 property
        private int reg76_Bit10_Bit8 = 3;
        [Required]
        [Range(0, 7)]
        public int Reg76_Bit10_Bit8
        {
            get => reg76_Bit10_Bit8;
            set
            {
                if (TrySetProperty(ref reg76_Bit10_Bit8, value, out _))
                {
                    Reg76_Bit10_Bit8Show = Math.Round(value * 0.8 + 0.6, 1);
                }
            }
        }
        [ObservableProperty]
        private double reg76_Bit10_Bit8Show = Math.Round(3 * 0.8 + 0.6, 1);
        public int Reg76_Bit10_Bit8Max { get; set; } = 7;
        public int Reg76_Bit10_Bit8Min { get; set; } = 0;

        // reg75_Bit7_Bit0 property
        private int reg75_Bit7_Bit0 = 19;
        [Required]
        [Range(0, 255)]
        public int Reg75_Bit7_Bit0
        {
            get => reg75_Bit7_Bit0;
            set
            {
                if (TrySetProperty(ref reg75_Bit7_Bit0, value, out _))
                {
                    Reg75_Bit7_Bit0Show = value * 40;
                    SwitchThreshold = Math.Round((value * 40) * Reg75_Bit15_Bit8 * 0.390625 / 1000, 1);
                }
            }
        }
        [ObservableProperty]
        private int reg75_Bit7_Bit0Show = 19 * 40;
        public int Reg75_Bit7_Bit0Max { get; set; } = 255;
        public int Reg75_Bit7_Bit0Min { get; set; } = 0;

        // reg75_Bit15_Bit8 property
        private int reg75_Bit15_Bit8 = 96;
        [Required]
        [Range(0, 255)]
        public int Reg75_Bit15_Bit8
        {
            get => reg75_Bit15_Bit8;
            set
            {
                if (TrySetProperty(ref reg75_Bit15_Bit8, value, out _))
                {
                    Reg75_Bit15_Bit8Show = Math.Round(value * 0.390625, 1);
                    SwitchThreshold = Math.Round((Reg75_Bit7_Bit0 * 40) * value * 0.390625 / 1000,1);
                }
            }
        }
        [ObservableProperty]
        private double reg75_Bit15_Bit8Show = Math.Round(96 * 0.390625, 1);
        public int Reg75_Bit15_Bit8Max { get; set; } = 255;
        public int Reg75_Bit15_Bit8Min { get; set; } = 0;

        // reg74_Bit7_Bit0 property
        private int reg74_Bit7_Bit0 = 30;
        [Required]
        [Range(0, 255)]
        public int Reg74_Bit7_Bit0
        {
            get => reg74_Bit7_Bit0;
            set
            {
                if (TrySetProperty(ref reg74_Bit7_Bit0, value, out _))
                {
                    Reg74_Bit7_Bit0Show = Math.Round(value * 0.1,2);
                }
            }
        }
        [ObservableProperty]
        private double reg74_Bit7_Bit0Show = 30 * 0.1;
        public int Reg74_Bit7_Bit0Max { get; set; } = 255;
        public int Reg74_Bit7_Bit0Min { get; set; } = 0;

        // reg74_Bit15_Bit8 property
        private int reg74_Bit15_Bit8 = 59;
        [Required]
        [Range(0, 255)]
        public int Reg74_Bit15_Bit8
        {
            get => reg74_Bit15_Bit8;
            set
            {
                if (TrySetProperty(ref reg74_Bit15_Bit8, value, out _))
                {
                    Reg74_Bit15_Bit8Show = value * 256;
                }
            }
        }
        [ObservableProperty]
        private int reg74_Bit15_Bit8Show = 59 * 256;
        public int Reg74_Bit15_Bit8Max { get; set; } = 255;
        public int Reg74_Bit15_Bit8Min { get; set; } = 0;

        // reg73_Bit8_Bit0 property
        private int reg73_Bit8_Bit0 = 38;
        [Required]
        [Range(0, 511)]
        public int Reg73_Bit8_Bit0
        {
            get => reg73_Bit8_Bit0;
            set
            {
                if (TrySetProperty(ref reg73_Bit8_Bit0, value, out _))
                {
                    Reg73_Bit8_Bit0Show = Math.Round(value * ((Reg83_Bit14 == 0 ? 19 : 38) / 511.0), 2);
                }
            }
        }
        [ObservableProperty]
        private double reg73_Bit8_Bit0Show = Math.Round(38 * (19 / 511.0), 2);
        public int Reg73_Bit8_Bit0Max { get; set; } = 511;
        public int Reg73_Bit8_Bit0Min { get; set; } = 0;

        // reg73_Bit15_Bit9 property
        private int reg73_Bit15_Bit9 = 111;
        [Required]
        [Range(0, 127)]
        public int Reg73_Bit15_Bit9
        {
            get => reg73_Bit15_Bit9;
            set
            {
                if (TrySetProperty(ref reg73_Bit15_Bit9, value, out _))
                {
                    Reg73_Bit15_Bit9Show = Math.Round(value * 0.78739, 1);
                }
            }
        }
        [ObservableProperty]
        private double reg73_Bit15_Bit9Show = Math.Round(111 * 0.78739, 1);
        public int Reg73_Bit15_Bit9Max { get; set; } = 127;
        public int Reg73_Bit15_Bit9Min { get; set; } = 0;

        // reg72_Bit9_Bit4 property
        private int reg72_Bit9_Bit4 = 0;
        [Required]
        [Range(0, 63)]
        public int Reg72_Bit9_Bit4
        {
            get => reg72_Bit9_Bit4;
            set
            {
                if (TrySetProperty(ref reg72_Bit9_Bit4, value, out _))
                {
                    Reg72_Bit9_Bit4Show = value == 0 ? "NO Duty Clamp" : Math.Round(value * 0.794, 1).ToString();
                }
            }
        }
        [ObservableProperty]
        private string reg72_Bit9_Bit4Show = Math.Round(0 * 0.794, 1).ToString();
        public int Reg72_Bit9_Bit4Max { get; set; } = 63;
        public int Reg72_Bit9_Bit4Min { get; set; } = 0;

        // reg72_Bit3_Bit0 property
        private int reg72_Bit3_Bit0 = 0;
        [Required]
        [Range(0, 15)]
        public int Reg72_Bit3_Bit0
        {
            get => reg72_Bit3_Bit0;
            set
            {
                if (TrySetProperty(ref reg72_Bit3_Bit0, value, out _))
                {
                    Reg72_Bit3_Bit0Show = Math.Round(100 - value * 1.56, 1);
                }
            }
        }
        [ObservableProperty]
        private double reg72_Bit3_Bit0Show = Math.Round(100 - 0 * 1.56, 1);
        public int Reg72_Bit3_Bit0Max { get; set; } = 15;
        public int Reg72_Bit3_Bit0Min { get; set; } = 0;

        // reg72_Bit15_Bit14 property
        private int reg72_Bit15_Bit14 = 0;
        [Required]
        [Range(0, 3)]
        public int Reg72_Bit15_Bit14
        {
            get => reg72_Bit15_Bit14;
            set
            {
                if (TrySetProperty(ref reg72_Bit15_Bit14, value, out _))
                {
                    Reg72_Bit15_Bit14Show = Math.Round((value + 1) * 0.8,2);
                }
            }
        }
        [ObservableProperty]
        private double reg72_Bit15_Bit14Show = (0 + 1) * 0.8;
        public int Reg72_Bit15_Bit14Max { get; set; } = 3;
        public int Reg72_Bit15_Bit14Min { get; set; } = 0;

        // reg72_Bit13_Bit10 property
        private int reg72_Bit13_Bit10 = 0;
        [Required]
        [Range(0, 15)]
        public int Reg72_Bit13_Bit10
        {
            get => reg72_Bit13_Bit10;
            set
            {
                if (TrySetProperty(ref reg72_Bit13_Bit10, value, out _))
                {
                    Reg72_Bit13_Bit10Show = value == 0 ? "Disabled" : (100 - (value - 1) * 1.56).ToString("0.0");
                }
            }
        }
        [ObservableProperty]
        private string reg72_Bit13_Bit10Show = "Disabled";
        public int Reg72_Bit13_Bit10Max { get; set; } = 15;
        public int Reg72_Bit13_Bit10Min { get; set; } = 0;

        // maxSpeed2 property
        private int maxSpeed2 = 128;
        [Required]
        [Range(0, 60000)]
        public int MaxSpeed2
        {
            get => maxSpeed2;
            set
            {
                if (value == 0) value = 1;
                if(value < Reg88_Bit11_Bit0) value = Reg88_Bit11_Bit0;
                if (TrySetProperty(ref maxSpeed2, value, out _))
                {
                    MaxSpeed2Show = value;
                }
            }
        }
        [ObservableProperty]
        private int maxSpeed2Show = 128;
        public int MaxSpeed2Max { get; set; } = 60000;
        public int MaxSpeed2Min { get; set; } = 0;

        // maxSpeed property
        private int maxSpeed = 10352;
        [Required]
        [Range(0, 60000)]
        public int MaxSpeed
        {
            get => maxSpeed;
            set
            {
                if (value == 0) value = 1;
                if (value < Reg81_Bit11_Bit0) value = Reg81_Bit11_Bit0 + 1;
                if ((value < Reg91) &SlpswrpmEnable) value = Reg91;
                if (TrySetProperty(ref maxSpeed, value, out _))
                {
                    MaxSpeedShow = value;
                    if (Reg82_Bit0 == 1)
                    {
                        ChartYAxisMax = MaxSpeed.ToString();
                    }
                }
            }
        }
        [ObservableProperty]
        private int maxSpeedShow = 9996;
        public int MaxSpeedMax { get; set; } = 60000;
        public int MaxSpeedMin { get; set; } = 0;

        #endregion

        #region RadioButton寄存器

        // reg84_Bit7 property
        private int reg84_Bit7 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg84_Bit7
        {
            get => reg84_Bit7;
            set => TrySetProperty(ref reg84_Bit7, value, out _);
        }
        public int Reg84_Bit7Max { get; set; } = 1;
        public int Reg84_Bit7Min { get; set; } = 0;

        // reg84_Bit6_Bit5 property
        private int reg84_Bit6_Bit5 = 1;
        [Required]
        [Range(0, 3)]
        public int Reg84_Bit6_Bit5
        {
            get => reg84_Bit6_Bit5;
            set
            {
                if(TrySetProperty(ref reg84_Bit6_Bit5, value, out _))
                {
                    EnableDecaySel = Reg84_Bit6_Bit5 == (int)StartMode.IPD_MODE1;
                    EnableIPD_Dtyramp = Reg84_Bit6_Bit5 == (int)StartMode.IPD_MODE2;
                }
            }
        }
        public int Reg84_Bit6_Bit5Max { get; set; } = 3;
        public int Reg84_Bit6_Bit5Min { get; set; } = 0;


        // reg84_Bit4 property
        private int reg84_Bit4 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg84_Bit4
        {
            get => reg84_Bit4;
            set => TrySetProperty(ref reg84_Bit4, value, out _);
        }
        public int Reg84_Bit4Max { get; set; } = 1;
        public int Reg84_Bit4Min { get; set; } = 0;

        // reg84_Bit3 property
        private int reg84_Bit3 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg84_Bit3
        {
            get => reg84_Bit3;
            set => TrySetProperty(ref reg84_Bit3, value, out _);
        }
        public int Reg84_Bit3Max { get; set; } = 1;
        public int Reg84_Bit3Min { get; set; } = 0;

        // reg84_Bit2 property
        private int reg84_Bit2 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg84_Bit2
        {
            get => reg84_Bit2;
            set => TrySetProperty(ref reg84_Bit2, value, out _);
        }
        public int Reg84_Bit2Max { get; set; } = 1;
        public int Reg84_Bit2Min { get; set; } = 0;

        // reg84_Bit0 property
        private int reg84_Bit0 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg84_Bit0
        {
            get => reg84_Bit0;
            set => TrySetProperty(ref reg84_Bit0, value, out _);
        }
        public int Reg84_Bit0Max { get; set; } = 1;
        public int Reg84_Bit0Min { get; set; } = 0;

        // reg83_Bit9_Bit8 property
        private int reg83_Bit9_Bit8 = 0;
        [Required]
        [Range(0, 3)]
        public int Reg83_Bit9_Bit8
        {
            get => reg83_Bit9_Bit8;
            set => TrySetProperty(ref reg83_Bit9_Bit8, value, out _);
        }
        public int Reg83_Bit9_Bit8Max { get; set; } = 3;
        public int Reg83_Bit9_Bit8Min { get; set; } = 0;

        // reg83_Bit7 property
        private int reg83_Bit7 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg83_Bit7
        {
            get => reg83_Bit7;
            set => TrySetProperty(ref reg83_Bit7, value, out _);
        }
        public int Reg83_Bit7Max { get; set; } = 1;
        public int Reg83_Bit7Min { get; set; } = 0;

        // reg83_Bit6 property
        private int reg83_Bit6 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg83_Bit6
        {
            get => reg83_Bit6;
            set => TrySetProperty(ref reg83_Bit6, value, out _);
        }
        public int Reg83_Bit6Max { get; set; } = 1;
        public int Reg83_Bit6Min { get; set; } = 0;

        // reg83_Bit5 property
        private int reg83_Bit5 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg83_Bit5
        {
            get => reg83_Bit5;
            set => TrySetProperty(ref reg83_Bit5, value, out _);
        }
        public int Reg83_Bit5Max { get; set; } = 1;
        public int Reg83_Bit5Min { get; set; } = 0;

        // reg83_Bit2 property
        private int reg83_Bit2 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg83_Bit2
        {
            get => reg83_Bit2;
            set => TrySetProperty(ref reg83_Bit2, value, out _);
        }
        public int Reg83_Bit2Max { get; set; } = 1;
        public int Reg83_Bit2Min { get; set; } = 0;

        // reg83_Bit15 property
        private int reg83_Bit15 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg83_Bit15
        {
            get => reg83_Bit15;
            set => TrySetProperty(ref reg83_Bit15, value, out _);
        }
        public int Reg83_Bit15Max { get; set; } = 1;
        public int Reg83_Bit15Min { get; set; } = 0;

        // reg83_Bit14 property
        private int reg83_Bit14 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg83_Bit14
        {
            get => reg83_Bit14;
            set => TrySetProperty(ref reg83_Bit14, value, out _);
        }
        public int Reg83_Bit14Max { get; set; } = 1;
        public int Reg83_Bit14Min { get; set; } = 0;

        // reg83_Bit13 property
        private int reg83_Bit13 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg83_Bit13
        {
            get => reg83_Bit13;
            set => TrySetProperty(ref reg83_Bit13, value, out _);
        }
        public int Reg83_Bit13Max { get; set; } = 1;
        public int Reg83_Bit13Min { get; set; } = 0;

        // reg83_Bit12 property
        private int reg83_Bit12 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg83_Bit12
        {
            get => reg83_Bit12;
            set
            {
                 if(TrySetProperty(ref reg83_Bit12, value, out _))
                {
                    EnableT_DITH = value == 1;
                    EnableSTP_DITH = value == 1;
                }
            }
        }
        public int Reg83_Bit12Max { get; set; } = 1;
        public int Reg83_Bit12Min { get; set; } = 0;

        // reg83_Bit11_Bit10 property
        private int reg83_Bit11_Bit10 = 0;
        [Required]
        [Range(0, 3)]
        public int Reg83_Bit11_Bit10
        {
            get => reg83_Bit11_Bit10;
            set => TrySetProperty(ref reg83_Bit11_Bit10, value, out _);
        }
        public int Reg83_Bit11_Bit10Max { get; set; } = 3;
        public int Reg83_Bit11_Bit10Min { get; set; } = 0;

        // reg83_Bit1 property
        private int reg83_Bit1 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg83_Bit1
        {
            get => reg83_Bit1;
            set => TrySetProperty(ref reg83_Bit1, value, out _);
        }
        public int Reg83_Bit1Max { get; set; } = 1;
        public int Reg83_Bit1Min { get; set; } = 0;

        // reg83_Bit0 property
        private int reg83_Bit0 = 1;
        [Required]
        [Range(0, 1)]
        public int Reg83_Bit0
        {
            get => reg83_Bit0;
            set => TrySetProperty(ref reg83_Bit0, value, out _);
        }
        public int Reg83_Bit0Max { get; set; } = 1;
        public int Reg83_Bit0Min { get; set; } = 0;

        // reg82_Bit9 property
        private int reg82_Bit9 = 1;
        [Required]
        [Range(0, 1)]
        public int Reg82_Bit9
        {
            get => reg82_Bit9;
            set => TrySetProperty(ref reg82_Bit9, value, out _);
        }
        public int Reg82_Bit9Max { get; set; } = 1;
        public int Reg82_Bit9Min { get; set; } = 0;

        // reg82_Bit8_Bit7 property
        private int reg82_Bit8_Bit7 = 1;
        [Required]
        [Range(0, 3)]
        public int Reg82_Bit8_Bit7
        {
            get => reg82_Bit8_Bit7;
            set => TrySetProperty(ref reg82_Bit8_Bit7, value, out _);
        }
        public int Reg82_Bit8_Bit7Max { get; set; } = 3;
        public int Reg82_Bit8_Bit7Min { get; set; } = 0;

        // reg82_Bit3 property
        private int reg82_Bit3 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg82_Bit3
        {
            get => reg82_Bit3;
            set => TrySetProperty(ref reg82_Bit3, value, out _);
        }
        public int Reg82_Bit3Max { get; set; } = 1;
        public int Reg82_Bit3Min { get; set; } = 0;

        // reg82_Bit2 property
        private int reg82_Bit2 = 1;
        [Required]
        [Range(0, 1)]
        public int Reg82_Bit2
        {
            get => reg82_Bit2;
            set => TrySetProperty(ref reg82_Bit2, value, out _);
        }
        public int Reg82_Bit2Max { get; set; } = 1;
        public int Reg82_Bit2Min { get; set; } = 0;

        // reg82_Bit15 property
        private int reg82_Bit15 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg82_Bit15
        {
            get => reg82_Bit15;
            set => TrySetProperty(ref reg82_Bit15, value, out _);
        }
        public int Reg82_Bit15Max { get; set; } = 1;
        public int Reg82_Bit15Min { get; set; } = 0;

        // reg82_Bit14 property
        private int reg82_Bit14 = 1;
        [Required]
        [Range(0, 1)]
        public int Reg82_Bit14
        {
            get => reg82_Bit14;
            set => TrySetProperty(ref reg82_Bit14, value, out _);
        }
        public int Reg82_Bit14Max { get; set; } = 1;
        public int Reg82_Bit14Min { get; set; } = 0;

        // reg82_Bit10 property
        private int reg82_Bit10 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg82_Bit10
        {
            get => reg82_Bit10;
            set => TrySetProperty(ref reg82_Bit10, value, out _);
        }
        public int Reg82_Bit10Max { get; set; } = 1;
        public int Reg82_Bit10Min { get; set; } = 0;

        // reg82_Bit1 property
        private int reg82_Bit1 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg82_Bit1
        {
            get => reg82_Bit1;
            set => TrySetProperty(ref reg82_Bit1, value, out _);
        }
        public int Reg82_Bit1Max { get; set; } = 1;
        public int Reg82_Bit1Min { get; set; } = 0;

        // reg82_Bit0 property
        private int reg82_Bit0 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg82_Bit0
        {
            get => reg82_Bit0;
            set
            {
                if(TrySetProperty(ref reg82_Bit0, value, out _))
                {
                    WeakReferenceMessenger.Default.Send<RPMInfoVisibleMesg, int>(new(Reg82_Bit0 != 0), (int)MesgToken.MainWindowToken);
                }
            }
        }
        public int Reg82_Bit0Max { get; set; } = 1;
        public int Reg82_Bit0Min { get; set; } = 0;

        // reg78_Bit7 property
        private int reg78_Bit7 = 0;
        [Required]
        [Range(0, 1)]
        public int Reg78_Bit7
        {
            get => reg78_Bit7;
            set => TrySetProperty(ref reg78_Bit7, value, out _);
        }
        public int Reg78_Bit7Max { get; set; } = 1;
        public int Reg78_Bit7Min { get; set; } = 0;
        #endregion

        #endregion

        #region Slider拖动触发事件
        // reg93_Bit7_Bit0 property 滑块触动事件
        [RelayCommand]
        private void Reg93_Bit7_Bit0DragCompleted()
        {
            ReadWrite.WriteReg(93, (Reg93_Bit15_Bit8 << 8) | Reg93_Bit7_Bit0, 2);
        }

        // reg93_Bit15_Bit8 property 滑块触动事件
        [RelayCommand]
        private void Reg93_Bit15_Bit8DragCompleted()
        {
            ReadWrite.WriteReg(93, (Reg93_Bit15_Bit8 << 8) | Reg93_Bit7_Bit0, 2);
        }

        // reg91 property 滑块触动事件
        [RelayCommand]
        private void Reg91DragCompleted()
        {
            ReadWrite.WriteReg(91, Reg91, 2);
            ReadWrite.WriteReg(80, Reg80(), 2);
            ReadWrite.WriteReg(89, Reg89(), 2);
            drawPlot();
        }

        // reg90_Bit5_Bit0 property 滑块触动事件
        [RelayCommand]
        private void Reg90_Bit5_Bit0DragCompleted()
        {
            ReadWrite.WriteReg(90, Reg90(), 2);
            drawPlot();
        }

        // reg90_Bit13_Bit6 property 滑块触动事件
        [RelayCommand]
        private void Reg90_Bit13_Bit6DragCompleted()
        {
            if (Reg90_Bit13_Bit6 > 0)
            {
                SlpswrpmEnable = true;
                MinSpeed2Enable = false;
                MaxSpeed2Enable = false;
                MinDtyclp2Enable = false;
                //radioButton_reg84_bit3_1.Enabled = false;
            }
            else
            {
                SlpswrpmEnable = false;
                //radioButton_reg84_bit3_1.Enabled = true;
            }

            if (Reg91 < Reg81_Bit11_Bit0)
            {
                Reg81_Bit11_Bit0 = Reg91;
                ReadWrite.WriteReg(81, Reg81(), 2);
            }
            Reg84_Bit2 = 0;
            Reg84_Bit3 = 0;
            SpeedVisible = false;
            ReadWrite.WriteReg(84, Reg84(), 2);
            ReadWrite.WriteReg(90, Reg90(), 2);
            ReadWrite.WriteReg(80, Reg80(), 2);
            ReadWrite.WriteReg(89, Reg89(), 2);
            drawPlot();
        }

        // reg88_Bit15_Bit12 property 滑块触动事件
        [RelayCommand]
        private void Reg88_Bit15_Bit12DragCompleted()
        {
            ReadWrite.WriteReg(88, Reg88(), 2);
        }

        // reg88_Bit11_Bit0 property 滑块触动事件
        [RelayCommand]
        private void Reg88_Bit11_Bit0DragCompleted()
        {
            ReadWrite.WriteReg(88, Reg88(), 2);
            ReadWrite.WriteReg(89, Reg89(), 2);
            drawPlot();
        }

        // reg87_Bit5_Bit0 property 滑块触动事件
        [RelayCommand]
        private void Reg87_Bit5_Bit0DragCompleted()
        {
            ReadWrite.WriteReg(87, (Reg87_Bit13_Bit8 << 8) | Reg87_Bit5_Bit0, 2);
            drawPlot();
        }

        // reg87_Bit13_Bit8 property 滑块触动事件
        [RelayCommand]
        private void Reg87_Bit13_Bit8DragCompleted()
        {
            ReadWrite.WriteReg(87, (Reg87_Bit13_Bit8 << 8) | Reg87_Bit5_Bit0, 2);
            drawPlot();
        }

        // reg86_Bit7_Bit0 property 滑块触动事件
        [RelayCommand]
        private void Reg86_Bit7_Bit0DragCompleted()
        {
            ReadWrite.WriteReg(86, (Reg86_Bit15_Bit8 << 8) | Reg86_Bit7_Bit0, 2);
            Set_Max_FG();
            drawPlot();
        }

        // reg86_Bit15_Bit8 property 滑块触动事件
        [RelayCommand]
        private void Reg86_Bit15_Bit8DragCompleted()
        {
            ReadWrite.WriteReg(86, (Reg86_Bit15_Bit8 << 8) | Reg86_Bit7_Bit0, 2);
            Set_Max_FG();
            drawPlot();
        }

        // reg85_Bit7_Bit0 property 滑块触动事件
        [RelayCommand]
        private void Reg85_Bit7_Bit0DragCompleted()
        {
            ReadWrite.WriteReg(85, (Reg85_Bit15_Bit8 << 8) | Reg85_Bit7_Bit0, 2);
            Set_Max_FG();
            drawPlot();
        }

        // reg85_Bit15_Bit8 property 滑块触动事件
        [RelayCommand]
        private void Reg85_Bit15_Bit8DragCompleted()
        {
            ReadWrite.WriteReg(85, (Reg85_Bit15_Bit8 << 8) | Reg85_Bit7_Bit0, 2);
            Set_Max_FG();
            drawPlot();
        }

        // reg84_Bit14_Bit9 property 滑块触动事件
        [RelayCommand]
        private void Reg84_Bit14_Bit9DragCompleted()
        {
            ReadWrite.WriteReg(84, Reg84(), 2);
            drawPlot();
        }

        // reg83_Bit4_Bit3 property 滑块触动事件
        [RelayCommand]
        private void Reg83_Bit4_Bit3DragCompleted()
        {
            ReadWrite.WriteReg(83, Reg83(), 2);
        }

        // reg82_Bit6_Bit4 property 滑块触动事件
        [RelayCommand]
        private void Reg82_Bit6_Bit4DragCompleted()
        {
            ReadWrite.WriteReg(82, Reg82(), 2);
            Set_Max_FG();
            drawPlot();
        }

        // reg82_Bit13_Bit12 property 滑块触动事件
        [RelayCommand]
        private void Reg82_Bit13_Bit12DragCompleted()
        {
            ReadWrite.WriteReg(82, Reg82(), 2);
        }

        // reg81_Bit15_Bit12 property 滑块触动事件
        [RelayCommand]
        private void Reg81_Bit15_Bit12DragCompleted()
        {
            ReadWrite.WriteReg(81, (Reg81_Bit15_Bit12 << 12) | Reg81_Bit11_Bit0, 2);
        }

        // reg81_Bit11_Bit0 property 滑块触动事件
        [RelayCommand]
        private void Reg81_Bit11_Bit0DragCompleted()
        {
            ReadWrite.WriteReg(81, Reg81(), 2);
            ReadWrite.WriteReg(80, Reg80(), 2);
        }

        // reg79_Bit7_Bit0 property 滑块触动事件
        [RelayCommand]
        private void Reg79_Bit7_Bit0DragCompleted()
        {
            ReadWrite.WriteReg(79, Reg79(), 2);
        }

        // reg79_Bit15_Bit8 property 滑块触动事件
        [RelayCommand]
        private void Reg79_Bit15_Bit8DragCompleted()
        {
            ReadWrite.WriteReg(79, (Reg79_Bit15_Bit8 << 8) | Reg79_Bit7_Bit0, 2);
        }

        // reg78_Bit6_Bit0 property 滑块触动事件
        [RelayCommand]
        private void Reg78_Bit6_Bit0DragCompleted()
        {
            ReadWrite.WriteReg(78, Reg78(), 2);
        }

        // reg78_Bit15_Bit8 property 滑块触动事件
        [RelayCommand]
        private void Reg78_Bit15_Bit8DragCompleted()
        {
            ReadWrite.WriteReg(78, Reg78(), 2);
        }

        // reg77_Bit7_Bit4 property 滑块触动事件
        [RelayCommand]
        private void Reg77_Bit7_Bit4DragCompleted()
        {
            ReadWrite.WriteReg(77, Reg77(), 2);
        }

        // reg77_Bit3_Bit0 property 滑块触动事件
        [RelayCommand]
        private void Reg77_Bit3_Bit0DragCompleted()
        {
            ReadWrite.WriteReg(77, Reg77(), 2);
        }

        // reg77_Bit15_Bit12 property 滑块触动事件
        [RelayCommand]
        private void Reg77_Bit15_Bit12DragCompleted()
        {
            ReadWrite.WriteReg(77, Reg77(), 2);
        }

        // reg77_Bit11_Bit8 property 滑块触动事件
        [RelayCommand]
        private void Reg77_Bit11_Bit8DragCompleted()
        {
            ReadWrite.WriteReg(77, Reg77(), 2);
        }

        // reg76_Bit7_Bit0 property 滑块触动事件
        [RelayCommand]
        private void Reg76_Bit7_Bit0DragCompleted()
        {
            setDCoff();
            ReadWrite.WriteReg(76, (Reg76_Bit10_Bit8 << 8) | Reg76_Bit7_Bit0, 2);
            drawPlot();
        }

        // reg76_Bit10_Bit8 property 滑块触动事件
        [RelayCommand]
        private void Reg76_Bit10_Bit8DragCompleted()
        {
            setDCoff();
            ReadWrite.WriteReg(76, (Reg76_Bit10_Bit8 << 8) | Reg76_Bit7_Bit0, 2);
            drawPlot();
        }

        // reg75_Bit7_Bit0 property 滑块触动事件
        [RelayCommand]
        private void Reg75_Bit7_Bit0DragCompleted()
        {
            ReadWrite.WriteReg(75, (Reg75_Bit15_Bit8 << 8) | Reg75_Bit7_Bit0, 2);
        }

        // reg75_Bit15_Bit8 property 滑块触动事件
        [RelayCommand]
        private void Reg75_Bit15_Bit8DragCompleted()
        {
            ReadWrite.WriteReg(75, (Reg75_Bit15_Bit8 << 8) | Reg75_Bit7_Bit0, 2);
        }

        // reg74_Bit7_Bit0 property 滑块触动事件
        [RelayCommand]
        private void Reg74_Bit7_Bit0DragCompleted()
        {
            ReadWrite.WriteReg(74, Reg74(), 2);
        }

        // reg74_Bit15_Bit8 property 滑块触动事件
        [RelayCommand]
        private void Reg74_Bit15_Bit8DragCompleted()
        {
            ReadWrite.WriteReg(74, (Reg74_Bit15_Bit8 << 8) & Reg74_Bit7_Bit0, 2);
            //ReadWrite.WriteReg(74, Reg74(), 2);
        }

        // reg73_Bit8_Bit0 property 滑块触动事件
        [RelayCommand]
        private void Reg73_Bit8_Bit0DragCompleted()
        {
            ReadWrite.WriteReg(73, (Reg73_Bit15_Bit9 << 9) | Reg73_Bit8_Bit0, 2);
        }

        // reg73_Bit15_Bit9 property 滑块触动事件
        [RelayCommand]
        private void Reg73_Bit15_Bit9DragCompleted()
        {
            ReadWrite.WriteReg(73, (Reg73_Bit15_Bit9 << 9) | Reg73_Bit8_Bit0, 2);
        }

        // reg72_Bit9_Bit4 property 滑块触动事件
        [RelayCommand]
        private void Reg72_Bit9_Bit4DragCompleted()
        {
            ReadWrite.WriteReg(72, Reg72(), 2);
            drawPlot();
        }

        // reg72_Bit3_Bit0 property 滑块触动事件
        [RelayCommand]
        private void Reg72_Bit3_Bit0DragCompleted()
        {
            ReadWrite.WriteReg(72, Reg72(), 2);
            drawPlot();
        }

        // reg72_Bit15_Bit14 property 滑块触动事件
        [RelayCommand]
        private void Reg72_Bit15_Bit14DragCompleted()
        {
            ReadWrite.WriteReg(72, Reg72(), 2);
        }

        // reg72_Bit13_Bit10 property 滑块触动事件
        [RelayCommand]
        private void Reg72_Bit13_Bit10DragCompleted()
        {
            ReadWrite.WriteReg(72, Reg72(), 2);
            drawPlot();
        }

        // maxSpeed2 property 滑块触动事件
        [RelayCommand]
        private void MaxSpeed2DragCompleted()
        {
            ReadWrite.WriteReg(89, Reg89(), 2);
            Set_Max_FG();
            drawPlot();
        }

        // maxSpeed property 滑块触动事件
        [RelayCommand]
        private void MaxSpeedDragCompleted()
        {
            ReadWrite.WriteReg(80, Reg80(), 2);
            Set_Max_FG();
            drawPlot();
        }
        #endregion

        #region RadioButton触发事件
        // reg84_Bit7 property 触动事件
        [RelayCommand]
        private void Reg84_Bit7Click()
        {
            ReadWrite.WriteReg(84, Reg84(), 2);
        }

        // reg84_Bit6_Bit5 property 触动事件
        [RelayCommand]
        private void Reg84_Bit6_Bit5Click()
        {
            ReadWrite.WriteReg(84, Reg84(), 2);

        }

        // reg84_Bit4 property 触动事件
        [RelayCommand]
        private void Reg84_Bit4Click()
        {
            ReadWrite.WriteReg(84, Reg84(), 2);
        }

        // reg84_Bit3 property 触动事件
        [RelayCommand]
        private void Reg84_Bit3Click()
        {
            if (Reg84_Bit3 == (int)Dir50.DISABLED)//0
            {
                Cmd_DisthEnable = true;
                MinDtyclp2Enable = false;
                MinSpeed2Enable = false;
                MaxSpeed2Enable = false;
                ReadWrite.WriteReg(84, Reg84(), 2);
                drawPlot();
            }
            else
            {

                if (Reg90_Bit13_Bit6 > 0 && SlpswdtyEnable)
                {
                    MessageBox.Show("Dual slopes cannot be set at the same time");
                    Reg84_Bit3 = (int)Dir50.DISABLED;
                }
                else
                {
                    Cmd_DisthEnable = false;
                    MinDtyclp2Enable = true;
                    if (Reg82_Bit0 == (int)Spd_Cl_En.OPENLOOP)
                    {
                        MinSpeed2Enable = false;
                        MaxSpeed2Enable = false;
                    }
                    else
                    {
                        MinSpeed2Enable = true;
                        MaxSpeed2Enable = true;
                    }
                    Reg84_Bit2 = (int)Start_En.DISABLED;
                    SpeedVisible = false;
                    ReadWrite.WriteReg(84, Reg84(), 2);

                    drawPlot();
                }
            }
        }

        // reg84_Bit2 property 触动事件
        [RelayCommand]
        private void Reg84_Bit2Click()
        {
            if (Reg84_Bit2 == (int)Start_En.DISABLED) //0
            {
                SpeedVisible = false;
                ReadWrite.WriteReg(84, Reg84(), 2);
                drawPlot();
            }
            else
            {
                SpeedVisible = true;

                Reg84_Bit3 = (int)Dir50.DISABLED;

                ReadWrite.WriteReg(84, Reg84(), 2);

                if (Reg90_Bit13_Bit6 > 0)
                {
                    Reg90_Bit13_Bit6 = 0;
                    ReadWrite.WriteReg(90, Reg90(), 2);
                    SlpswrpmEnable = false;
                }
                drawPlot();
            }
        }

        // reg84_Bit0 property 触动事件
        [RelayCommand]
        private void Reg84_Bit0Click()
        {
            ReadWrite.WriteReg(84, Reg84(), 2);
            drawPlot();
        }

        // reg83_Bit9_Bit8 property 触动事件
        [RelayCommand]
        private void Reg83_Bit9_Bit8Click()
        {
            ReadWrite.WriteReg(83, Reg83(), 2);
        }

        // reg83_Bit7 property 触动事件
        [RelayCommand]
        private void Reg83_Bit7Click()
        {
            ReadWrite.WriteReg(83, Reg83(), 2);
        }

        // reg83_Bit6 property 触动事件
        [RelayCommand]
        private void Reg83_Bit6Click()
        {
            ReadWrite.WriteReg(83, Reg83(), 2);
        }

        // reg83_Bit5 property 触动事件
        [RelayCommand]
        private void Reg83_Bit5Click()
        {
            ReadWrite.WriteReg(83, Reg83(), 2);
        }

        // reg83_Bit2 property 触动事件
        [RelayCommand]
        private void Reg83_Bit2Click()
        {
            ReadWrite.WriteReg(83, Reg83(), 2);
        }

        // reg83_Bit15 property 触动事件
        [RelayCommand]
        private void Reg83_Bit15Click()
        {
            ReadWrite.WriteReg(83, Reg83(), 2);
            Reg73_Bit8_Bit0Show = Math.Round(Reg73_Bit8_Bit0 * ((Reg83_Bit15 == (int)VBB_RNG.V_19 ? 19 : 38) / 511.0), 2);
        }

        // reg83_Bit14 property 触动事件
        [RelayCommand]
        private void Reg83_Bit14Click()
        {
            ReadWrite.WriteReg(83, Reg83(), 2);
        }

        // reg83_Bit13 property 触动事件
        [RelayCommand]
        private void Reg83_Bit13Click()
        {
            ReadWrite.WriteReg(83, Reg83(), 2);
        }

        // reg83_Bit12 property 触动事件
        [RelayCommand]
        private void Reg83_Bit12Click()
        {
            ReadWrite.WriteReg(83, Reg83(), 2);
        }

        // reg83_Bit11_Bit10 property 触动事件
        [RelayCommand]
        private void Reg83_Bit11_Bit10Click()
        {
            ReadWrite.WriteReg(83, Reg83(), 2);
        }

        // reg83_Bit1 property 触动事件
        [RelayCommand]
        private void Reg83_Bit1Click()
        {
            ReadWrite.WriteReg(83, Reg83(), 2);
        }

        // reg83_Bit0 property 触动事件
        [RelayCommand]
        private void Reg83_Bit0Click()
        {
            ReadWrite.WriteReg(83, Reg83(), 2);
        }

        // reg82_Bit9 property 触动事件
        [RelayCommand]
        private void Reg82_Bit9Click()
        {
            ReadWrite.WriteReg(82, Reg82(), 2);
        }

        // reg82_Bit8_Bit7 property 触动事件
        [RelayCommand]
        private void Reg82_Bit8_Bit7Click()
        {
            ReadWrite.WriteReg(82, Reg82(), 2);
        }

        // reg82_Bit3 property 触动事件
        [RelayCommand]
        private void Reg82_Bit3Click()
        {
            ReadWrite.WriteReg(82, Reg82(), 2);
        }

        // reg82_Bit2 property 触动事件
        [RelayCommand]
        private void Reg82_Bit2Click()
        {
            ReadWrite.WriteReg(82, Reg82(), 2);
        }

        // reg82_Bit15 property 触动事件
        [RelayCommand]
        private void Reg82_Bit15Click()
        {
            ReadWrite.WriteReg(82, Reg82(), 2);
        }

        // reg82_Bit14 property 触动事件
        [RelayCommand]
        private void Reg82_Bit14Click()
        {
            ReadWrite.WriteReg(82, Reg82(), 2);
        }

        // reg82_Bit10 property 触动事件
        [RelayCommand]
        private void Reg82_Bit10Click()
        {
            ReadWrite.WriteReg(82, Reg82(), 2);
        }

        // reg82_Bit1 property 触动事件
        [RelayCommand]
        private void Reg82_Bit1Click()
        {
            ReadWrite.WriteReg(82, Reg82(), 2);
        }

        // reg82_Bit0 property 触动事件
        [RelayCommand]
        private void Reg82_Bit0Click()
        {
            if (Reg82_Bit0 == (int)Spd_Cl_En.OPENLOOP)//0
            {
                MinSpeedEnable = false;
                MaxDtyclpEnable = false;
                SlpswdtyEnable = false;
                MinSpeed2Enable = false;
                MaxSpeed2Enable = false;
                MinDtyclp2Enable = false;
                SlpswrpmEnable = false;
                EnableDir50 = true;


                ReadWrite.WriteReg(82, Reg82(), 2);
                if (Reg90_Bit13_Bit6 > 0)
                {
                    Reg90_Bit13_Bit6 = 0;
                    ReadWrite.WriteReg(90, Reg90(), 2);
                }
                drawPlot();
            }
            else
            {
                if (Reg84_Bit3 == (int)Dir50.ENABLE)
                {
                    MinSpeed2Enable = true;
                    MaxSpeed2Enable = true;
                    MinDtyclp2Enable = true;
                }
                MinSpeedEnable = true;
                MaxDtyclpEnable = true;
                SlpswdtyEnable = true;
                EnableDir50 = false;


                if (Reg90_Bit13_Bit6 > 0)
                {
                    Reg84_Bit3 = (int)Dir50.DISABLED;
                    SlpswrpmEnable = true;
                }
                ReadWrite.WriteReg(82, Reg82(), 2);

                ChartYAxisMax = MaxSpeed.ToString();
            }
        }

        // reg78_Bit7 property 触动事件
        [RelayCommand]
        private void Reg78_Bit7Click()
        {
            ReadWrite.WriteReg(78, Reg78(), 2);
            ReadWrite.WriteReg(84, Reg84(), 2);
        }
        #endregion


        #region 图表相关的属性和事件函数
        /// <summary>
        /// 定义让图表曲线刷新的事件
        /// </summary>
        public event Action SpeedCurveUpdate;
        [ObservableProperty]
        private string chartYAxisMax = "Max";

        public double DC_off;
        public void setDCoff()
        {
            DC_off = checked(1.0 * (double)(Reg76_Bit7_Bit0 * 2) / 511.0 - 1.0 * (double)(Reg76_Bit10_Bit8 * 4 + 3) / 511.0);
            if (DC_off < 0.0)
            {
                DC_off = 0.0;
            }
        }

        public void drawPlot()
        {
            if (MaxSpeed <= Reg81_Bit11_Bit0 || (Reg84_Bit3 == 1 & (MaxSpeed2 <= Reg88_Bit11_Bit0)))
            {
                return;
            }
            SpeedCurveUpdate?.Invoke();

            int num = 0;
            int num2 = 0;
            int num3 = 0;
            int num4 = 0;
            int num5 = 0;
            if (Reg84_Bit2 == 1)
            {
                num = 8160;
                num2 = 0;
                num3 = 8160;
                num4 = 0;
                num5 = num;
            }
            else if (Reg82_Bit0 == 0)
            {
                num = 1000;
                num2 = 0;
                num3 = 1000;
                num4 = 0;
                num5 = num;
            }
            else
            {
                num = MaxSpeed;
                num2 = Reg81_Bit11_Bit0;
                num3 = MaxSpeed2;
                num4 = Reg88_Bit11_Bit0;
                num5 = num;
                if (Reg84_Bit3 == 1 && num3 > num5)
                {
                    num5 = num3;
                }
            }
            int num6 = MainWindowVM.MainDutyCycleValue;
            checked
            {
                if (Reg84_Bit0 == 1)
                {
                    num6 = MainWindowVM.MainDutyCycleValueMax - num6;
                }
                int num7 = (int)Math.Round(getYpoint((double)num6 / (double)MainWindowVM.MainDutyCycleValueMax, isSinglePoint: true) * (double)num5);
                double num8 = (double)(num7 * 2) / 60.0;
                double num9 = num8 * ((double)(Reg82_Bit6_Bit4 + 1) / 2.0);
                if (num7 < 0)
                {
                    WeakReferenceMessenger.Default.Send<UpdateRPMInfoMesg, int>(new("Open loop"), (int)MesgToken.MainWindowToken);
                }
                else
                {
                    WeakReferenceMessenger.Default.Send<UpdateRPMInfoMesg, int>(
                        new("RPM = " + num7.ToString("N0") + "\r\nFG = " + num8.ToString("N0") + " Hz  \r\nFelec. = " + num9.ToString("N0") + " Hz"),
                        (int)MesgToken.MainWindowToken);
                }
            }
        }


        #endregion

        #region 寄存器公用处理函数
        public int Reg80()
        {
            int regData = 0;
            if (Reg90_Bit13_Bit6 == 0)
            {
                if ((double)(64 * (MaxSpeed - Reg81_Bit11_Bit0)) / 511.0 > 16383.0)
                {
                    regData = 16383;
                }
                else
                {
                    regData = (int)((long)Math.Round((double)(64 * (MaxSpeed - Reg81_Bit11_Bit0)) / 511.0) & 0x3FFF);
                }
            }
            else if ((double)(64 * (Reg91 - Reg81_Bit11_Bit0)) / (double)(Reg90_Bit13_Bit6 * 2) > 16383.0)
            {
                regData = 16383;
            }
            else
            {
                regData = (int)((long)Math.Round((double)(64 * (Reg91 - Reg81_Bit11_Bit0)) / (double)(Reg90_Bit13_Bit6 * 2)) & 0x3FFF);
            }
            return regData;
        }
        public int Reg77() => (Reg77_Bit15_Bit12 << 12) | (Reg77_Bit11_Bit8 << 8) | (Reg77_Bit7_Bit4 << 4) | Reg77_Bit3_Bit0;

        public int Reg81() => (Reg81_Bit15_Bit12 << 12) | Reg81_Bit11_Bit0;

        private void Set_Max_FG()
        {
            checked
            {
                int PolePairs = Reg82_Bit6_Bit4 + 1;

                if (Reg84_Bit2 == 1)
                {
                    int value = Reg85_Bit7_Bit0;
                    value = Math.Max(value, Reg85_Bit15_Bit8);
                    value = Math.Max(value, Reg86_Bit7_Bit0);
                    value = Math.Max(value, Reg86_Bit15_Bit8);
                    double num = (double)(value * 32) * 1.25;
                    double num2 = num * (double)(PolePairs) / 60.0;
                    double num4 = (double)25000000 / (num2 * 2048.0);
                    Reg79_Bit7_Bit0 = (int)Math.Round(Math.Max(Reg79_Bit7_Bit0Min, Math.Min(Reg79_Bit7_Bit0Max, num4)));

                    ReadWrite.WriteReg(79, Reg79(), 2);
                }
                else
                {
                    double num5 = (double)MaxSpeed * 1.25;
                    if (Reg84_Bit3 == 1 && (MaxSpeed2 > MaxSpeed))
                    {
                        num5 = MaxSpeed2 * 1.25;
                    }
                    double maxElecf = num5 * (double)(PolePairs) / 60.0;
                    int num7 = 25000000;
                    double maxElecfCounts = (double)num7 / (maxElecf * 2048.0);

                    Reg79_Bit7_Bit0 = (int)Math.Round(Math.Max(0, Math.Min(255, maxElecfCounts)));

                    ReadWrite.WriteReg(79, Reg79(), 2);
                }
            }
        }

        public int Reg84() => (Reg84_Bit14_Bit9 << 9) | (1 << 8) | (Reg84_Bit7 << 7)
                      | (Reg84_Bit6_Bit5 << 5) | (Reg84_Bit4 << 4) | (Reg84_Bit3 << 3) | (Reg84_Bit2 << 2) | (1 << 1) | Reg84_Bit0;

        public int Reg79() => (Reg79_Bit15_Bit8 << 8) | Reg79_Bit7_Bit0;

        public int Reg72() => (Reg72_Bit15_Bit14 << 14) | (Reg72_Bit13_Bit10 << 10) | (Reg72_Bit9_Bit4 << 4) | Reg72_Bit3_Bit0;

        public int Reg82() => (Reg82_Bit15 << 15) | (Reg82_Bit14 << 14) | (Reg82_Bit13_Bit12 << 12) | (0 << 11)
                      | (Reg82_Bit10 << 10) | (Reg82_Bit9 << 9) | (Reg82_Bit8_Bit7 << 7) | (Reg82_Bit6_Bit4 << 4)
                      | (Reg82_Bit3 << 3) | (Reg82_Bit2 << 2) | (Reg82_Bit1 << 1) | Reg82_Bit0;

        public int Reg90() => (Reg90_Bit13_Bit6 << 6) | Reg90_Bit5_Bit0;

        public int Reg74() => ((int)(MaxSpeed * (255.0 / 60000)) << 8) | Reg74_Bit7_Bit0;

        public int Reg88() => (Reg88_Bit15_Bit12 << 12) | Reg88_Bit11_Bit0;

        public int Reg89()
        {
            int regData = 0;

            if (Reg90_Bit13_Bit6 > 0)
            {
                if ((double)(64 * (MaxSpeed - Reg91)) / (double)(511 - Reg90_Bit13_Bit6 * 2) > 16383.0)
                {
                    regData = 16383;
                }
                else
                {
                    regData = (int)((long)Math.Round((double)(64 * (MaxSpeed - Reg91)) / (double)(511 - Reg90_Bit13_Bit6 * 2)) & 0x3FFF);
                }
            }
            if (Reg84_Bit3 == 1)
            {
                if ((double)(64 * (MaxSpeed2 - Reg88_Bit11_Bit0)) / 511.0 > 16383.0)
                {
                    regData = 16383;
                }
                else
                {
                    regData = (int)((long)Math.Round((double)(64 * (MaxSpeed2 - Reg88_Bit11_Bit0)) / 511.0) & 0x3FFF);
                }
            }

            return regData;
        }

        public int Reg83() => (Reg83_Bit15 << 15) | (Reg83_Bit14 << 14) | (Reg83_Bit13 << 13) | (Reg83_Bit12 << 12)
                      | (Reg83_Bit11_Bit10 << 10) | (Reg83_Bit9_Bit8 << 8) | (Reg83_Bit7 << 7) | (Reg83_Bit6 << 6)
                      | (Reg83_Bit5 << 5) | (Reg83_Bit4_Bit3 << 3) | (Reg83_Bit2 << 2) | (Reg83_Bit1 << 1) | Reg83_Bit0;
        

        public int Reg78() => (Reg78_Bit15_Bit8 << 8) | (Reg78_Bit7 << 7) | Reg78_Bit6_Bit0;

        public double getYpoint(double input, bool isSinglePoint = false)
        {
            double num = 1.0;
            int num2 = 0;
            int num3 = 0;
            int num4 = 0;
            int num5 = 0;
            int num6 = 0;
            if (Reg84_Bit2 == 1)
            {
                num2 = 8160;
                num3 = 0;
                num4 = 8160;
                num5 = 0;
                num6 = num2;
            }
            else if (Reg82_Bit0 == 0)
            {
                num2 = 1000;
                num3 = 0;
                num4 = 1000;
                num5 = 0;
                num6 = num2;
            }
            else
            {
                num2 = MaxSpeed;
                num3 = Reg81_Bit11_Bit0;
                num4 = MaxSpeed2;
                num5 = Reg88_Bit11_Bit0;
                num6 = num2;
                if (Reg84_Bit3 == 1 && num4 > num6)
                {
                    num6 = num4;
                }
            }
            checked
            {
                if (isSinglePoint)
                {
                    if (Reg84_Bit3 == 1)
                    {
                        bool flag = (input < 0.5 + DC_off) & (input > 0.5 - DC_off);
                        bool flag2 = (input < 0.5 + 1.0 * (double)(Reg76_Bit7_Bit0 * 2) / 511.0) & (input > 0.5 - 1.0 * (double)(Reg76_Bit7_Bit0 * 2) / 511.0);
                        if (flag | (none_on_off == 2 && flag2))
                        {
                            return 0.0;
                        }
                    }
                    else
                    {
                        bool flag3 = input < DC_off;
                        bool flag4 = input < 1.0 * (double)(Reg76_Bit7_Bit0 * 2) / 511.0;
                        if (flag3 | (none_on_off == 2 && flag4))
                        {
                            return 0.0;
                        }
                    }
                }
                if (Reg84_Bit3 == 0 && input > 1.0 - (double)(Reg72_Bit13_Bit10 - 1) * 0.0156)
                {
                    return 0.0;
                }
                double num7 = (double)(Reg72_Bit9_Bit4 * 4) / 511.0;
                double num8 = (double)(Reg90_Bit5_Bit0 * 4) / 511.0;
                if (Reg84_Bit3 == 1)
                {
                    if (input >= 0.5 && input < 0.5 + num7)
                    {
                        return getYpoint(0.5 + num7);
                    }
                    if (input < 0.5 && input > 0.5 - num8)
                    {
                        return getYpoint(0.5 - num8);
                    }
                }
                else if (input < num7)
                {
                    return getYpoint(num7);
                }
                double num9 = 0.01 * (800.0 * (double)Reg72_Bit3_Bit0 / 511.0);
                if (Reg84_Bit3 == 1 && num7 > 0.5 - num9)
                {
                    //showLabel117 = true;
                    return 0.0;
                }
                if (Reg84_Bit3 == 1)
                {
                    if (((input < num9) | (input > 1.0 - num9)) & Reg82_Bit0 != 0)
                    {
                        if (InputDutyGreaterThanMaxDutyClamp_MaxDutyClamp)
                        {
                            if (input < 0.5)
                            {
                                return getYpoint(num9);
                            }
                            return getYpoint(1.0 - num9);
                        }
                        if (input < 0.5)
                        {
                            return 0.0 - (1.0 - input * 2.0);
                        }
                        return 0.0 - (input * 2.0 - 1.0);
                    }
                }
                else if ((input > 1.0 - num9) & Reg82_Bit0 != 0)
                {
                    if (InputDutyGreaterThanMaxDutyClamp_MaxDutyClamp)
                    {
                        return getYpoint(1.0 - num9);
                    }
                    return 0.0 - input;
                }
                if (Reg84_Bit2 == 1)
                {
                    if (input >= (double)(Reg84_Bit14_Bit9 * 8 + 7) / 511.0)
                    {
                        return (double)Reg86_Bit15_Bit8 / (double)Reg86_Bit15_Bit8Max;
                    }
                    if (input >= (double)(Reg87_Bit13_Bit8 * 8 + 7) / 511.0)
                    {
                        return (double)Reg86_Bit7_Bit0 / (double)Reg86_Bit7_Bit0Max;
                    }
                    if (input >= (double)(Reg87_Bit5_Bit0 * 8 + 7) / 511.0)
                    {
                        return (double)Reg85_Bit15_Bit8 / (double)Reg85_Bit15_Bit8Max;
                    }
                    return (double)Reg85_Bit7_Bit0 / (double)Reg85_Bit7_Bit0Max;
                }
                num = (Reg84_Bit3 == 1 ? ((!(input < 0.5)) ? (num * (2.0 * (input - 0.5) * (double)(num2 - num3) + (double)num3)) : (num * (2.0 * (0.5 - input) * (double)(num4 - num5) + (double)num5))) : (((Reg90_Bit13_Bit6 == 0) | Reg82_Bit0 == 0) ? (num * (1.0 * input * (double)(num2 - num3) + (double)num3)) : ((input < (double)Reg90_Bit13_Bit6 / 255.0) ? (num * (input / ((double)Reg90_Bit13_Bit6 / 255.0) * (double)(Reg91 - num3) + (double)num3)) : (((double)Reg90_Bit13_Bit6 / 255.0 != 1.0) ? (num * ((input - (double)Reg90_Bit13_Bit6 / 255.0) / (1.0 - (double)Reg90_Bit13_Bit6 / 255.0) * (double)(num2 - Reg91) + (double)Reg91)) : (num * (double)num2)))));
                return num / (double)num6;
            }
        }

        #endregion

    }
}
